﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using hsgsSHXX.DBUtility;//Please add references
using System.Collections.Generic;
namespace hsgsSHXX.DAL
{
	/// <summary>
	/// 数据访问类:qyzl
	/// </summary>
	public partial class qyzl
	{
		public qyzl()
		{}
		#region  Method



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hsgsSHXX.Model.qyzl model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into qyzl(");
			strSql.Append("xm,xb,zw,lxdh1,lxdh2,lxdh3,dw,xzqh,dz)");
			strSql.Append(" values (");
			strSql.Append("@xm,@xb,@zw,@lxdh1,@lxdh2,@lxdh3,@dw,@xzqh,@dz)");
			SqlParameter[] parameters = {
					new SqlParameter("@xm", SqlDbType.NVarChar,255),
					new SqlParameter("@xb", SqlDbType.Int,4),
					new SqlParameter("@zw", SqlDbType.NVarChar,255),
					new SqlParameter("@lxdh1", SqlDbType.NVarChar,255),
					new SqlParameter("@lxdh2", SqlDbType.NVarChar,255),
					new SqlParameter("@lxdh3", SqlDbType.NVarChar,255),
					new SqlParameter("@dw", SqlDbType.NVarChar,255),
					new SqlParameter("@xzqh", SqlDbType.NVarChar,255),
					new SqlParameter("@dz", SqlDbType.NVarChar,255)};
			parameters[0].Value = model.xm;
			parameters[1].Value = model.xb;
			parameters[2].Value = model.zw;
			parameters[3].Value = model.lxdh1;
			parameters[4].Value = model.lxdh2;
			parameters[5].Value = model.lxdh3;
			parameters[6].Value = model.dw;
			parameters[7].Value = model.xzqh;
			parameters[8].Value = model.dz;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hsgsSHXX.Model.qyzl model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update qyzl set ");
			strSql.Append("xm=@xm,");
			strSql.Append("xb=@xb,");
			strSql.Append("zw=@zw,");
			strSql.Append("lxdh1=@lxdh1,");
			strSql.Append("lxdh2=@lxdh2,");
			strSql.Append("lxdh3=@lxdh3,");
			strSql.Append("dw=@dw,");
			strSql.Append("xzqh=@xzqh,");
			strSql.Append("dz=@dz");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
					new SqlParameter("@xm", SqlDbType.NVarChar,255),
					new SqlParameter("@xb", SqlDbType.Int,4),
					new SqlParameter("@zw", SqlDbType.NVarChar,255),
					new SqlParameter("@lxdh1", SqlDbType.NVarChar,255),
					new SqlParameter("@lxdh2", SqlDbType.NVarChar,255),
					new SqlParameter("@lxdh3", SqlDbType.NVarChar,255),
					new SqlParameter("@dw", SqlDbType.NVarChar,255),
					new SqlParameter("@xzqh", SqlDbType.NVarChar,255),
					new SqlParameter("@dz", SqlDbType.NVarChar,255)};
			parameters[0].Value = model.xm;
			parameters[1].Value = model.xb;
			parameters[2].Value = model.zw;
			parameters[3].Value = model.lxdh1;
			parameters[4].Value = model.lxdh2;
			parameters[5].Value = model.lxdh3;
			parameters[6].Value = model.dw;
			parameters[7].Value = model.xzqh;
			parameters[8].Value = model.dz;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from qyzl ");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
};

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hsgsSHXX.Model.qyzl GetModel()
		{
			//该表无主键信息，请自定义主键/条件字段
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 xm,xb,zw,lxdh1,lxdh2,lxdh3,dw,xzqh,dz from qyzl ");
			strSql.Append(" where ");
			SqlParameter[] parameters = {
};

			hsgsSHXX.Model.qyzl model=new hsgsSHXX.Model.qyzl();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["xm"]!=null && ds.Tables[0].Rows[0]["xm"].ToString()!="")
				{
					model.xm=ds.Tables[0].Rows[0]["xm"].ToString();
				}
				if(ds.Tables[0].Rows[0]["xb"]!=null && ds.Tables[0].Rows[0]["xb"].ToString()!="")
				{
					model.xb=int.Parse(ds.Tables[0].Rows[0]["xb"].ToString());
				}
				if(ds.Tables[0].Rows[0]["zw"]!=null && ds.Tables[0].Rows[0]["zw"].ToString()!="")
				{
					model.zw=ds.Tables[0].Rows[0]["zw"].ToString();
				}
				if(ds.Tables[0].Rows[0]["lxdh1"]!=null && ds.Tables[0].Rows[0]["lxdh1"].ToString()!="")
				{
					model.lxdh1=ds.Tables[0].Rows[0]["lxdh1"].ToString();
				}
				if(ds.Tables[0].Rows[0]["lxdh2"]!=null && ds.Tables[0].Rows[0]["lxdh2"].ToString()!="")
				{
					model.lxdh2=ds.Tables[0].Rows[0]["lxdh2"].ToString();
				}
				if(ds.Tables[0].Rows[0]["lxdh3"]!=null && ds.Tables[0].Rows[0]["lxdh3"].ToString()!="")
				{
					model.lxdh3=ds.Tables[0].Rows[0]["lxdh3"].ToString();
				}
				if(ds.Tables[0].Rows[0]["dw"]!=null && ds.Tables[0].Rows[0]["dw"].ToString()!="")
				{
					model.dw=ds.Tables[0].Rows[0]["dw"].ToString();
				}
				if(ds.Tables[0].Rows[0]["xzqh"]!=null && ds.Tables[0].Rows[0]["xzqh"].ToString()!="")
				{
					model.xzqh=ds.Tables[0].Rows[0]["xzqh"].ToString();
				}
				if(ds.Tables[0].Rows[0]["dz"]!=null && ds.Tables[0].Rows[0]["dz"].ToString()!="")
				{
					model.dz=ds.Tables[0].Rows[0]["dz"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select xm,xb,zw,lxdh1,lxdh2,lxdh3,dw,xzqh,dz ");
			strSql.Append(" FROM qyzl ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" xm,xb,zw,lxdh1,lxdh2,lxdh3,dw,xzqh,dz ");
			strSql.Append(" FROM qyzl ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM qyzl ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T. desc");
			}
			strSql.Append(")AS Row, T.*  from qyzl T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "qyzl";
			parameters[1].Value = "";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
        public List<Model.qyzl> GetFondListByPage(int pageSize, int pageIndex, string strWhere, string fOrderFiled, out int total)
        {
            if (fOrderFiled == "")
            {
                fOrderFiled = "xm desc";
            }
            SqlParameter[] parameters = {  
                    new SqlParameter("@tableName", SqlDbType.VarChar, 50),  
                    new SqlParameter("@fields", SqlDbType.VarChar, 2000),  
                    new SqlParameter("@orderField", SqlDbType.VarChar, 500),  
                    new SqlParameter("@sqlWhere", SqlDbType.VarChar,2000),  
                    new SqlParameter("@pageSize", SqlDbType.Int),  
                    new SqlParameter("@pageIndex", SqlDbType.Int),  
                    new SqlParameter("@TotalPage", SqlDbType.Int),
                    new SqlParameter("@TotalRecordCount", SqlDbType.Int) 
                    };

            parameters[0].Value = "qyzl";
            parameters[1].Value = "*";
            parameters[2].Value = fOrderFiled;
            parameters[3].Value = strWhere;
            parameters[4].Value = pageSize;
            parameters[5].Value = pageIndex;
            parameters[6].Direction = ParameterDirection.Output;
            parameters[7].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("up_GetRecordByPage", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[7].Value.ToString());
            return DataTableToList(ds.Tables[0]);

        }
        public List<hsgsSHXX.Model.qyzl> DataTableToList(DataTable dt)
        {
            List<hsgsSHXX.Model.qyzl> modelList = new List<hsgsSHXX.Model.qyzl>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                hsgsSHXX.Model.qyzl model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new hsgsSHXX.Model.qyzl();
                    if (dt.Rows[n]["xm"] != null && dt.Rows[n]["xm"].ToString() != "")
                    {
                        model.xm = dt.Rows[n]["xm"].ToString();
                    }
                    if (dt.Rows[n]["xb"] != null && dt.Rows[n]["xb"].ToString() != "")
                    {
                        model.xb = int.Parse(dt.Rows[n]["xb"].ToString());
                    }
                    if (dt.Rows[n]["zw"] != null && dt.Rows[n]["zw"].ToString() != "")
                    {
                        model.zw = dt.Rows[n]["zw"].ToString();
                    }
                    if (dt.Rows[n]["lxdh1"] != null && dt.Rows[n]["lxdh1"].ToString() != "")
                    {
                        model.lxdh1 = dt.Rows[n]["lxdh1"].ToString();
                    }
                    if (dt.Rows[n]["lxdh2"] != null && dt.Rows[n]["lxdh2"].ToString() != "")
                    {
                        model.lxdh2 = dt.Rows[n]["lxdh2"].ToString();
                    }
                    if (dt.Rows[n]["lxdh3"] != null && dt.Rows[n]["lxdh3"].ToString() != "")
                    {
                        model.lxdh3 = dt.Rows[n]["lxdh3"].ToString();
                    }
                    if (dt.Rows[n]["dw"] != null && dt.Rows[n]["dw"].ToString() != "")
                    {
                        model.dw = dt.Rows[n]["dw"].ToString();
                    }
                    if (dt.Rows[n]["xzqh"] != null && dt.Rows[n]["xzqh"].ToString() != "")
                    {
                        model.xzqh = dt.Rows[n]["xzqh"].ToString();
                    }
                    if (dt.Rows[n]["dz"] != null && dt.Rows[n]["dz"].ToString() != "")
                    {
                        model.dz = dt.Rows[n]["dz"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }
	}
}

